package com.chalmers.calculator;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View;
import android.view.View.OnClickListener;

public class CalculatorActivity extends Activity {


	//operator
	private String opera = "";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);



		//+
		Button a = (Button) findViewById(R.id.button1);
		a.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				opera = "+";
				calculatorProcess(opera);
			}
		}); 

		//-
		Button b = (Button) findViewById(R.id.button2);
		b.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				opera = "-";
				calculatorProcess(opera);
			}
		}); 

		//*
		Button c = (Button) findViewById(R.id.button3);
		c.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				opera = "*";
				calculatorProcess(opera);
			}
		}); 

		///
		Button d = (Button) findViewById(R.id.button4);
		d.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				opera = "/";
				calculatorProcess(opera);
			}
		}); 
	}
	public void calculatorProcess(String op){

		final TextView resultat = (TextView)findViewById(R.id.textView2);

		EditText txt1 = (EditText)findViewById(R.id.editText1);
		EditText txt2 = (EditText)findViewById(R.id.editText2);

		String firstNo = txt1.getText().toString();
		String secondNo = txt2.getText().toString();

		double sum = 0;
		try {
			if(op == "+")
				sum = Double.parseDouble(firstNo) + Double.parseDouble(secondNo);	
			else if(op.equals("-"))
				sum = Double.parseDouble(firstNo) - Double.parseDouble(secondNo);	
			else if(op.equals("*"))
				sum = Double.parseDouble(firstNo) * Double.parseDouble(secondNo);	
			else if(op.equals("/"))
				sum = Double.parseDouble(firstNo) / Double.parseDouble(secondNo);	

		}catch(NumberFormatException nfe){
			System.out.println("Parse fail: " + nfe);	
		}
		resultat.setText("" + sum);
	}
}